import request from "@/utils/request";
import { SkuSpecValueForm } from "@/api/sku/spec/value/sku-spec-value";
import { ProductSpuForm } from "@/api/spu/product-spu";
import { WarehouseForm, WarehousePageQuery } from "@/api/warehouse/warehouse";

const PRODUCTSKU_BASE_URL = "/api/v1/product-sku";

const ProductSkuAPI = {
  /** 获取商品库存单元(SKU)分页数据 */
  getPage(queryParams?: ProductSkuPageQuery) {
    return request<any, PageResult<ProductSkuPageVO[]>>({
      url: `${PRODUCTSKU_BASE_URL}/page`,
      method: "get",
      params: queryParams,
    });
  },
  /**
   * 获取商品库存单元(SKU)表单数据
   *
   * @param id 商品库存单元(SKU)ID
   * @returns 商品库存单元(SKU)表单数据
   */
  getFormData(id: number) {
    return request<any, ProductSkuForm>({
      url: `${PRODUCTSKU_BASE_URL}/${id}/form`,
      method: "get",
    });
  },


  /**
   * 批量删除商品库存单元(SKU)，多个以英文逗号(,)分割
   *
   * @param ids 商品库存单元(SKU)ID字符串，多个以英文逗号(,)分割
   */
  deleteByIds(ids: string) {
    return request({
      url: `${PRODUCTSKU_BASE_URL}/${ids}`,
      method: "delete",
    });
  },
  listSpu: (queryParams?: ProductSkuPageQuery) => {
    return request<any, ProductSkuForm[]>({
      url: `${PRODUCTSKU_BASE_URL}/listSpu`,
      method: "get",
      params: queryParams,
    });
  },

  listWarehouseForm: (queryParams?: WarehousePageQuery) => {
    return request<any, WarehouseForm[]>({
      url: `${PRODUCTSKU_BASE_URL}/listWarehouseForm`,
      method: "get",
      params: queryParams,
    });
  },
  listSkuSpecValueForm(id: number) {
    return request<any, SkuSpecValueForm[]>({
      url: `${PRODUCTSKU_BASE_URL}/listSkuSpecValue/${id}`,
      method: "get",
    });
  },
  // 在ProductSkuAPI对象中添加
  getListSpuSkuSpecGroupSpecValueVo(id: number) {
    return request<any, SPKGV>({
      url: `${PRODUCTSKU_BASE_URL}/getListSpuSkuSpecGroupSpecValueVo/${id}`,
      method: "get",
    });
  },
};

export default ProductSkuAPI;

/** 商品库存单元(SKU)分页查询参数 */
export interface ProductSkuPageQuery extends PageQuery {
  /** 所属SPU */
  spuId?: number;
  /** SKU编码 */
  skuCode?: string;
  /** 销售价格 */
  price?: number;
  /** 成本价 */
  costPrice?: number;
  /** 库存数量 */
  stock?: number;
  /** 库存预警值 */
  stockThreshold?: number;
  /** 条形码 */
  barCode?: string;
  /** 重量(kg) */
  weight?: number;
  /** 体积(L) */
  volume?: number;
  /** 状态 */
  status?: number;
  /** 创建时间 */
  createTime?: [string, string];
  skuId?: number;
  warehouseId?: number;
  skuMeasurement?: string;
}

/** 商品库存单元(SKU)表单对象 */
export interface ProductSkuForm {
  /** SKU主键 */
  id?: number;
  /** 所属SPU */
  spuId?: number;
  /** 销售价格 */
  price?: number;
  /** 成本价 */
  costPrice?: number;
  /** 库存数量 */
  stock: number;
  /** 库存预警值 */
  stockThreshold?: number;
  /** 条形码 */
  barCode?: string;
  /** 重量(kg) */
  weight?: number;
  /** 体积(L) */
  volume?: number;
  /** SKU主图 */
  mainImage?: string;
  /** SKU详情图 */
  detailImages?: string;
  /** 状态 */
  status?: number;
  /** 创建时间 */
  createTime?: Date;

  warehouseId?: number;
  skuMeasurement?: string;
  stockFlowType?: number;
  quantity: number;
  skuRatio: number;
}

/** 商品库存单元(SKU)分页对象 */
export interface ProductSkuPageVO {
  /** SKU主键 */
  id?: number;
  /** 所属SPU */
  spuId?: number;
  /** SKU编码 */
  skuCode?: string;
  /** 销售价格 */
  price?: number;
  /** 成本价 */
  costPrice?: number;
  /** 库存数量 */
  stock?: number;
  /** 库存预警值 */
  stockThreshold?: number;
  /** 条形码 */
  barCode?: string;
  /** 重量(kg) */
  weight?: number;
  /** 体积(L) */
  volume?: number;
  /** SKU主图 */
  mainImage?: string;
  /** SKU详情图 */
  detailImages?: string;
  /** 状态 */
  status?: number;
  /** 创建时间 */
  createTime?: Date;
  skuMeasurement?: string;
  skuRatio?: number;
}

export interface ProductSkuConnectSpecValueForm {
  /** Sku主键 */
  id?: number;
  specValueIds?: number[];
  specValues: {
    [specGroupId: number]: number | null;
  };
}

// 在product-sku.ts中添加
export interface SpuSkuSpecGroupSpecValueVo {
  spuForm?: ProductSpuForm;
  skuForm?: ProductSkuForm;
  specGroupValueVoList?: SpecGroupValueVo[];
}

export interface SV{
  specValues: SkuSpecValueForm[];
}

export interface SPKGV {
  all?:SpuSkuSpecGroupSpecValueVo[];
  sv?:SV[];
}

export interface SpecGroupValueVo {
  specGroupForm?: SpecGroupForm;
  specValueForms?: SpecValueForm[];
}

export interface SpecGroupForm {
  id?: number | string;
  groupName?: string;
  categoryId?: number;
}

export interface SpecValueForm {
  id?: number;
  specGroupId?: number;
  valueName?: string;
}
